<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="460px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-left: 55px"
      >
        <el-col :span="24">
          <el-form-item label="物料代码" prop="bomWldm">
            <el-input
              v-model="queryParams.bomWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态标志" prop="bomZtbz">
            <el-select
              v-model="queryParams.bomZtbz"
              placeholder="请选择状态标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in bomZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购属性" prop="bomCgsx">
            <el-select
              v-model="queryParams.bomCgsx"
              placeholder="请选择采购属性"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in bomCgsxOptions"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="16" style="display: flex; align-items: flex-start">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearchs"
          >搜索</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:bom_check:edit']"
        >
          修改
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleCheck"
          v-hasPermi="['project:bom_check:check']"
        >
          审核
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleRollback"
          v-hasPermi="['project:bom_check:rollback']"
        >
          撤回
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:bom:export']"
        >
          导出
        </el-button>
        <Tag type="dot" color="#29abdf">新增</Tag>
        <Tag type="dot" color="#06c420">提交</Tag>
        <Tag type="dot" color="#bb0331">审核</Tag>
        <Tag type="dot" color="#d39207">变更</Tag>
      </el-col>
      <el-col :span="8" style="text-align: right">
        <Page
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <split-pane
      :min-percent="10"
      :default-percent="50"
      split="horizontal"
      style="height: calc(100vh - 200px)"
    >
      <template #paneL>
        <el-table
          :row-style="iRowStyle"
          :cell-style="iCellStyle"
          :header-row-style="iHeaderRowStyle"
          :header-cell-style="iHeaderCellStyle"
          v-loading="loading"
          stripe
          border
          size="mini"
          highlight-current-row
          height="100%"
          :data="bomList"
          ref="multipleTable"
          @row-click="getChildList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            fixed="left"
            type="selection"
            width="55"
            align="center"
          />
          <el-table-column
            label="物料代码"
            show-overflow-tooltip
            align="center"
            width="135px"
            prop="bomWldm"
          />

          <el-table-column
            label="客户编码"
            width="125"
            align="center"
            prop="itmCustwldm"
          />

          <el-table-column
            label="品名规格"
            show-overflow-tooltip
            width="350"
            prop="itmPmgg"
          />

          <el-table-column
            label="采购属性"
            width="80"
            show-overflow-tooltip
            align="center"
            prop="bomCgsx"
          >
            <template #default="scope">
              <dict-tag :options="bomCgsxOptions" :value="scope.row.bomCgsx" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            width="80"
            show-overflow-tooltip
            align="center"
            prop="remark"
          />

          <el-table-column
            label="撤回理由"
            width="80"
            show-overflow-tooltip
            align="center"
            prop="bomNgdesc"
          />

          <el-table-column
            label="单据状态"
            width="80"
            align="center"
            prop="bomZtbz"
          >
            <template #default="scope">
              <dict-tag :options="bomZtbzOptions" :value="scope.row.bomZtbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="创建人员"
            width="80"
            align="center"
            prop="createBy"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="修改人员"
            width="80"
            align="center"
            prop="updateBy"
          />
          <el-table-column
            label="修改时间"
            align="center"
            prop="updateTime"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            width="80"
            align="center"
            prop="bomShry"
          />
          <el-table-column
            label="审核日期"
            align="center"
            prop="bomShrq"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.bomShrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #paneR>
        <!-- 子项Table-->
        <el-table
          :row-style="iRowStyle"
          :row-class-name="rowClass"
          :cell-style="iCellStyle"
          :height="tableH"
          :header-row-style="iHeaderRowStyle"
          :header-cell-style="iHeaderCellStyle"
          style="width: 100%"
          height="100%"
          stripe
          border
          size="mini"
          highlight-current-row
          v-loading="loading"
          :data="childBomList"
        >
          <el-table-column
            fixed
            label="层级"
            width="55"
            align="center"
            prop="level"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="bodZxdm"
          />
          <el-table-column
            label="客户编码"
            width="125px"
            show-overflow-tooltip
            align="center"
            prop="itmCustomeWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="350"
            prop="itmPmgg"
          />
          <el-table-column label="单位" width="80" align="center" prop="itmDw">
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购属性"
            width="80"
            align="center"
            prop="bodCgsx"
          >
            <template #default="scope">
              <dict-tag :options="bomCgsxOptions" :value="scope.row.bodCgsx" />
            </template>
          </el-table-column>
          <el-table-column
            label="单位用量"
            width="100"
            align="center"
            prop="bodDwyl"
          />
          <el-table-column
            label="单件净重"
            width="100"
            align="center"
            prop="itmJz"
          />
          <el-table-column
            label="单件水口"
            width="100"
            align="center"
            prop="itmSkzl"
          />
          <el-table-column
            label="单件毛重"
            width="100"
            align="center"
            prop="itmMz"
          />
          <el-table-column
            label="备注"
            width="100"
            show-overflow-tooltip
            align="center"
            prop="remark"
          />
          <el-table-column
            label="主成型标识"
            width="100"
            align="center"
            prop="bodMain"
          >
            <template #default="scope">
              <dict-tag :options="bodWwbzOptions" :value="scope.row.bodMain" />
            </template>
          </el-table-column>
          <el-table-column
            label="委外发料标识"
            width="100"
            align="center"
            prop="bodWwbz"
          >
            <template #default="scope">
              <dict-tag :options="bodWwbzOptions" :value="scope.row.bodWwbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="变更单号"
            width="100"
            align="center"
            prop="bodBgdh"
          />
          <el-table-column
            label="ENC单号"
            width="115"
            align="center"
            prop="bodEcnno"
          />
          <el-table-column
            label="变更备注"
            width="115"
            align="center"
            prop="bodDesc"
          />
        </el-table>
      </template>
    </split-pane>

    <!-- 添加或修改bom资料维护对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料代码" prop="bomWldm">
          <el-input v-model="form.bomWldm" placeholder="请输入物料代码" />
        </el-form-item>
        <el-form-item label="品名规格" prop="bomPmgg">
          <el-input v-model="form.itmPmgg" disabled />
        </el-form-item>
        <el-form-item label="采购属性" prop="bomCgsx">
          <el-select v-model="form.bomCgsx" placeholder="请选择采购属性">
            <el-option
              v-for="dict in bomCgsxOptionsNromal"
              :key="dict.cgId"
              :label="dict.cgName"
              :value="dict.cgId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 撤回对话框 -->
    <el-dialog
      :title="title"
      v-model="rollbackView"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form
        ref="rollbackForm"
        :model="rollbackForm"
        :rules="rollbackFormRules"
        label-width="100px"
      >
        <el-form-item label="撤回理由" prop="bomNgdesc">
          <el-input
            v-model="rollbackForm.bomNgdesc"
            placeholder="请输入撤回理由"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelRollback">取 消</el-button>
        <el-button type="primary" @click="submitRollbackForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBom,
  getBom,
  delBom,
  addBom,
  updateBom,
  exportBom,
  addChildBom,
  getChild,
  checkBom,
  RollbackBom,
} from "@/api/project/bom";
import { getAllCgList, getAllCgListNormal } from "@/api/project/mstr";

export default {
  name: "BomCheck",
  data() {
    return {
      tableH: 1,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      Fxwldms: [],
      itmDwOptions: [],
      bodWwbzOptions: [],
      //子项添加
      addChildView: false,
      rollbackView: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // bom资料维护表格数据
      bomList: [],
      //子项Bom
      childBomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 状态标志字典
      bomZtbzOptions: [],
      //采购属性字典
      bomCgsxOptions: [],
      //有效采购属性
      bomCgsxOptionsNromal: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomWldm: null,
        itmPmgg: null,
        bomZtbz: "1",
        bomCgsx: null,
        bomTjflag: "1",
      },
      // 表单参数
      form: {},
      //孩子表单
      childForm: {},

      //单据是否已经审核
      bomZtbz: null,

      //撤回表单
      rollbackForm: {},
      // 表单校验
      rules: {
        bomWldm: [
          { required: true, message: "物料代码不能为空", trigger: "blur" },
        ],
        bomCgsx: [
          { required: true, message: "采购属性不能为空", trigger: "change" },
        ],
      },
      childFormRules: {
        bodZxdm: [
          { required: true, message: "子项代码不能为空", trigger: "blur" },
        ],
        bodCgsx: [
          { required: true, message: "采购属性不能为空", trigger: "change" },
        ],
        bodDwyl: [
          { required: true, message: "单位用量不能为空", trigger: "blur" },
        ],
      },
      rollbackFormRules: {
        bomNgdesc: [
          { required: true, message: "撤回理由不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_bom_ztbz").then((response) => {
      this.bomZtbzOptions = response.data;
    });
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });

    this.getDicts("sys_flag").then((response) => {
      this.bodWwbzOptions = response.data;
    });

    //将不是字典的集合转为字典
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });

    getAllCgListNormal().then((response) => {
      this.bomCgsxOptionsNromal = response.data;
      for (let i of this.bomCgsxOptionsNromal) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
  },
  methods: {
    rowClass({ row, rowIndex }) {
      if (row.bodZtbz === "2" && row.bodBgdh) return "update";
      if (row.bodZtbz === "1") return "insert";
    },
    iRowStyle: function ({ row, rowIndex }) {
      return "height:10px";
    },
    iHeaderRowStyle: function ({ row, rowIndex }) {
      return "height:10px";
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //新增状态
      if (row.bomTjflag === "0" && row.bomZtbz === 1) {
        return "padding:0px 0;color:rgb(0, 126, 255)";
      }
      //提交状态
      else if (row.bomTjflag === "1" && row.bomZtbz === 1) {
        return "padding:0px 0;color:rgb(15, 152, 86)";
      }
      //审核状态
      else if (row.bomZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      } else {
        return "padding:0px 0";
      }
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    iHeaderCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      return "padding:0px;text-align:center";
    },
    /** 查询bom资料维护列表 */
    getList() {
      this.loading = true;
      this.tableH = (document.body.clientHeight - 160) / 2;
      this.queryParams.pageSize = this.tableH > 350 ? 15 : 10;
      listBom(this.queryParams).then((response) => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.childBomList = [];
        if (this.bomList != null && this.bomList.length > 0) {
          this.getChildList(this.bomList[0]);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    cancelRollback() {
      this.rollbackView = false;
      this.reset();
    },
    handleSearchs() {
      this.reset();
      this.title = "查询bom资料审核";
      this.openSearch = true;
    },
    // 表单重置
    reset() {
      this.form = {
        bomId: null,
        bomWldm: null,
        bomZtbz: 1,
        bomTjflag: null,
        bomCgsx: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bomShry: null,
        bomShrq: null,
        remark: null,
        itmPmgg: null,
      };
      this.resetForm("form");

      this.childForm = {
        bodFxdm: null,
        bodZxdm: null,
        bodDwyl: null,
        bodCgsx: null,
        bodPmgg: null,
        remark: null,
        bomPmgg: null,
      };
      this.resetForm("childForm");

      this.rollbackForm = {
        bomId: null,
        bomNgdesc: null,
      };
      this.resetForm("rollbackForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bomId);
      this.Fxwldms = selection.map((item) => item.bomWldm);
      this.bomZtbz = selection.map((item) => item.bomZtbz);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加bom资料维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bomId = row.bomId || this.ids;
      getBom(bomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改bom资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bomId != null) {
            updateBom(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBom(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    //查询子项
    getChildList(row) {
      //点击行触发，选中或不选中复选框
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
      if (row.bomWldm != null) {
        getChild(row.bomWldm).then((response) => {
          this.childBomList = response.data;
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bomIds = row.bomId || this.ids;
      this.$confirm(
        '是否确认删除bom资料维护编号为"' + bomIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有bom资料维护数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportBom(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    //审核
    handleCheck(row) {
      const bomIds = row.bomId || this.ids;

      const bomWldm = row.bomWldm || this.Fxwldms;

      this.$confirm(
        '是否确认审核物料代码为"' + bomWldm + '"的数据项,一旦确认，将无法撤回?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return checkBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功");
        })
        .catch(() => {});
    },
    //撤回
    handleRollback(row) {
      if (this.bomZtbz[0] === 2) {
        this.msgError("单据已经审核，不允许撤回");

        return;
      }
      this.reset();
      const bomWldm = row.bomWldm || this.Fxwldms;

      this.$confirm(
        '是否确认撤回物料代码为"' + bomWldm + '"的数据项,一旦确认，将无法撤回?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {})
        .then(() => {
          this.rollbackView = true;
          this.title = "请填写撤回理由";
        })
        .catch(() => {});
    },
    //提交撤回理由
    submitRollbackForm() {
      this.$refs["rollbackForm"].validate((valid) => {
        if (valid) {
          this.rollbackForm.bomId = this.ids[0];
          RollbackBom(this.rollbackForm).then((response) => {
            this.msgSuccess("撤回成功");
            this.rollbackView = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
